#include "createview.h"
#include "ui_createview.h"
#include <QMessageBox>

CreateView::CreateView(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::CreateView)
{
    ui->setupUi(this);
    QObject::connect(ui->buttonCreate, SIGNAL(clicked()), this, SLOT(confirm()));
}

CreateView::~CreateView()
{
    delete ui;
}

void CreateView::changeEvent(QEvent *e)
{
    QFrame::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void CreateView::confirm()
{
    if(ui->lineName->text().isEmpty() || ui->lineRegistration->text().isEmpty() ||
        ui->lineDescription->text().isEmpty() || ui->linePass->text().isEmpty() ||
        ui->lineCofirm->text().isEmpty())
    {
        QMessageBox::critical(this, QString::fromUtf8("ERRO"),
                                 QString::fromUtf8("Todos os campos devem ser preenchidos."));
    } else if(ui->lineCofirm->text() != ui->linePass->text())
    {
        QMessageBox::critical(this, QString::fromUtf8("ERRO"),
                              QString::fromUtf8("Asenha não é igual à confirmação."));
    } else if((ui->admin->isChecked() && !ui->user->isChecked()) &&
              (!ui->admin->isChecked() && ui->user->isChecked()))
    {
        QMessageBox::critical(this, QString::fromUtf8("ERRO"),
                              QString::fromUtf8("Selecione o tipo de usuário."));
    } else
    {
        QString type;
        if(ui->admin->isChecked()){
            type = "admin";
        } else
        {
            type = "user";
        }
        emit this->create(ui->lineName->text(), type, ui->lineDescription->text(),
                          ui->lineRegistration->text(), ui->linePass->text());
    }
}

void CreateView::clearFields()
{
    ui->lineName->clear();
    ui->lineRegistration->clear();
    ui->lineDescription->clear();
    ui->linePass->clear();
    ui->lineCofirm->clear();
}
